package com.magicalcoder.youyamvc.web.controller.web.logadminoperate;
import com.magicalcoder.youyamvc.app.logadminoperate.service.LogAdminOperateService;
import com.magicalcoder.youyamvc.app.logadminoperate.service.LogAdminOperateProxyService;
import com.magicalcoder.youyamvc.app.logadminoperate.constant.LogAdminOperateConstant;
import com.magicalcoder.youyamvc.app.logadminoperate.util.LogAdminOperateUtil;
import com.magicalcoder.youyamvc.web.controller.web.logadminoperate.param.LogAdminOperateRequest;
import com.magicalcoder.youyamvc.web.controller.web.logadminoperate.param.LogAdminOperateResponse;
import com.magicalcoder.youyamvc.app.model.LogAdminOperate;
import com.magicalcoder.youyamvc.core.common.utils.ProjectUtil;
import com.magicalcoder.youyamvc.core.common.utils.MapUtil;
import com.magicalcoder.youyamvc.core.common.utils.ListUtils;
import com.magicalcoder.youyamvc.core.common.utils.StringUtils;
import com.magicalcoder.youyamvc.core.common.dto.AjaxData;
import com.magicalcoder.youyamvc.core.common.utils.copy.Copyer;
import com.magicalcoder.youyamvc.core.spring.admin.AdminLoginController;
import com.magicalcoder.youyamvc.core.spring.web.WebLoginController;
import com.magicalcoder.youyamvc.web.common.BaseController;
import com.magicalcoder.youyamvc.app.userweb.util.UserWebUtil;
import com.magicalcoder.youyamvc.core.common.exception.BusinessException;

import org.springframework.format.annotation.DateTimeFormat;
import java.util.*;
import java.math.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMethod;

/**
* 代码为自动生成 Created by www.magicalcoder.com
* 如果你改变了此类 read 请将此行删除
* 欢迎加入官方QQ群:323237052
*/

@RequestMapping({"/web/log_admin_operate"})
@Controller
public class LogAdminOperateListController extends BaseController
{
    @Resource
    private LogAdminOperateService logAdminOperateService;
    @Resource
    private LogAdminOperateProxyService logAdminOperateProxyService;

    /** method:GET
    *   url:/web/log_admin_operate/list/{pageIndex}/{pageSize}?...
    *   demo:/web/log_admin_operate/list/1/20
    *   获取后台操作日志分页数据
    *   是否需要登录                          是
    *是否必须   入参                          注释
    *  是   @pageIndex                     (Integer)当前页码 [1,n]
    *  是   @pageSize                      (Integer)每页条数 [1,n]
    *  否   @callback                      (String)回调方法
    *  否   @orderBySqlField               (String)排序字段
    *  否   @descAsc                       (String)desc|asc
    *  否   @encode                        (String)编码 默认UTF-8
    *  否   @id                     (Long)主键 
    *  否   @adminUserId                     (Long)管理员 
    *  否   @userName                     (String)管理员名称 
    *  否   @tableName                     (String)表名 
    *  否   @operateType                     (String)操作类型 
    *  否   @url                     (String)链接 
    *  否   @primaryIdValue                     (String)主键值 
    *  否   @formBody                     (String)提交内容 
    *  否   @createTime                     (Date)创建时间 
    *  否  in @adminUserIdFirst                     (Long)管理员 
    *  否  like @userNameFirst                     (String)管理员名称 
    *  否  like @urlFirst                     (String)链接 
    *  否  like @formBodyFirst                     (String)提交内容 
    *  否  >= @createTimeFirst                     (Date)创建时间 
    *  否  <= @createTimeSecond                  (Date)创建时间 
    *  返回
    *   {
    *      code:0,message:"",jsonp:"",
    *      info:
                [{
    *              id         (Long)主键 
    *              adminUserId         (Long)管理员 
    *              userName         (String)管理员名称 
    *              tableName         (String)表名 
    *              operateType         (String)操作类型 
    *              url         (String)链接 
    *              primaryIdValue         (String)主键值 
    *              formBody         (String)提交内容 
    *              createTime         (Date)创建时间 
    *      }]
    *   }
    */
    @RequestMapping(value={"list/{pageIndex}/{pageSize}"}, method={RequestMethod.GET})
    public void list(@PathVariable Integer pageIndex,@PathVariable Integer pageSize,
        @RequestParam(required=false, value="orderBySqlField") String orderBySqlField,
        @RequestParam(required=false, value="descAsc") String descAsc,
        @RequestParam(required=false, value="callback") String callback,
        @RequestParam(required=false, value="encode") String encode,
        @RequestParam(required = false,value ="id")   Long id,
        @RequestParam(required = false,value ="adminUserId")   Long adminUserId,
        @RequestParam(required = false,value ="userName")   String userName,
        @RequestParam(required = false,value ="tableName")   String tableName,
        @RequestParam(required = false,value ="operateType")   String operateType,
        @RequestParam(required = false,value ="url")   String url,
        @RequestParam(required = false,value ="primaryIdValue")   String primaryIdValue,
        @RequestParam(required = false,value ="formBody")   String formBody,
        @RequestParam(required = false,value ="createTime")      @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date createTime,
        @RequestParam(required = false,value ="adminUserIdFirst")                            String adminUserIdFirst ,
        @RequestParam(required = false,value ="userNameFirst")                            String userNameFirst ,
        @RequestParam(required = false,value ="urlFirst")                            String urlFirst ,
        @RequestParam(required = false,value ="formBodyFirst")                            String formBodyFirst ,
        @RequestParam(required = false,value ="createTimeFirst")                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date createTimeFirst ,
        @RequestParam(required = false,value ="createTimeSecond")                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date createTimeSecond ,
        HttpServletRequest request,HttpServletResponse response,ModelMap modelMap)
    {
        try{
            String orderBySqlFieldStr = ",id,admin_user_id,user_name,table_name,operate_type,url,primary_id_value,form_body,create_time,";
            String orderBy = ProjectUtil.filterOrderBy(orderBySqlFieldStr,orderBySqlField,descAsc);
            if(StringUtils.isBlank(orderBy)){
                orderBy = "";
            }
            pageSize = Math.min(LogAdminOperateConstant.PAGE_MAX_SIZE,pageSize);
            int idx = (pageIndex.intValue() - 1) * pageSize;
            Long _userId = UserWebUtil.userId(request);
            Map<String,Object> query = MapUtil.buildMap("orderBy", orderBy,
         "id",id,
         "adminUserId",adminUserId,
         "userName",userName,
         "tableName",tableName,
         "operateType",operateType,
         "url",url,
         "primaryIdValue",primaryIdValue,
         "formBody",formBody,
         "createTime",createTime,
                    "adminUserIdFirst",ProjectUtil.toList(adminUserIdFirst ,Long.class),
                    "userNameFirst",userNameFirst ,
                    "urlFirst",urlFirst ,
                    "formBodyFirst",formBodyFirst ,
                    "createTimeFirst",createTimeFirst ,
                    "createTimeSecond",createTimeSecond ,
                "start",idx,"limit", pageSize ,"userId",_userId);

            List<LogAdminOperate> pageList = this.logAdminOperateService.getLogAdminOperateList(query);
            toWebSuccessJson(callback,encode,response, coverToResponseList(pageList));
        }catch(Exception e){
            String exceptionMsg = ProjectUtil.buildExceptionMsg(e.getMessage());
            toWebExceptionJson(response,exceptionMsg);
        }
    }

    private List<LogAdminOperateResponse> coverToResponseList(List<LogAdminOperate> pageList){
        if(ListUtils.isBlank(pageList)){
            return null;
        }
        return Copyer.copy(pageList,LogAdminOperateResponse.class);
    }


        /** method:GET
        *   url:/user/log_admin_operate/item?...
        *   demo:/user/log_admin_operate/item
    *   获取后台操作日志详情数据
    *   是否需要登录                          是
    *是否必须   入参                          注释
    *  否   @callback                      (String)回调方法
    *  否   @encode                        (String)编码 默认UTF-8
     *  否  @id        (Long) 主键
    *  返回
    *   {
    *      code:0,message:"",jsonp:"",
    *      info:
                {
    *              id         (Long)主键 
    *              adminUserId         (Long)管理员 
    *              userName         (String)管理员名称 
    *              tableName         (String)表名 
    *              operateType         (String)操作类型 
    *              url         (String)链接 
    *              primaryIdValue         (String)主键值 
    *              formBody         (String)提交内容 
    *              createTime         (Date)创建时间 
    *      }
    *   }
    */
    @RequestMapping(value={"item"}, method={RequestMethod.GET})
    public void item(
        @RequestParam(required=false, value="callback") String callback,
        @RequestParam(required=false, value="encode") String encode,
        @RequestParam(required = false,value ="id")Long id,
        HttpServletRequest request,HttpServletResponse response,ModelMap modelMap)
    {
        try{
            Long _userId = UserWebUtil.userId(request);
            Map<String,Object> query = MapUtil.buildMap(
                     "id",id,
                "userId",_userId
            );
            LogAdminOperate logAdminOperate = this.logAdminOperateService.selectFirstLogAdminOperate(query);
            toWebSuccessJson(callback,encode,response, Copyer.copy(logAdminOperate,new LogAdminOperateResponse()));
        }catch(Exception e){
            String exceptionMsg = ProjectUtil.buildExceptionMsg(e.getMessage());
            toWebExceptionJson(response,exceptionMsg);
        }
    }

}